package com.hs.jhzd.service.task;

import com.hs.jhzd.entity.Task;
import com.hs.jhzd.repository.PrisonerAccountRepository;
import com.hs.jhzd.repository.PrisonerRepository;
import com.hs.jhzd.repository.TaskRepository;
import com.hs.jhzd.service.message.NoticeProducer;
import it.sauronsoftware.cron4j.Scheduler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ScheduleService {

  private Scheduler scheduler;

  @Autowired
  private NoticeProducer noticeProducer;

  @Autowired
  private PrisonerAccountRepository prisonerAccountRepository;

  @Autowired
  private PrisonerRepository prisonerRepository;

  @Autowired
  private TaskRepository taskRepository;

  @Autowired
  private CacheManager cacheManager;

  @Value("${mq.switch}")
  private Boolean needMessage;

  @Value("${jsbh}")
  private String jsbh;

  public boolean start() {
    scheduler = new Scheduler();
    if (!scheduler.isStarted()) {
      scheduler.start();
      log.info("ScheduleService started");
    } else {
      log.info("ScheduleService has already started");
    }
    return true;
  }


  public it.sauronsoftware.cron4j.Task getById(@NotBlank String id) {
    return scheduler.getTask(id);
  }

  private String addTask(String cronExpress, Runnable task, String order) {
    String taskId = scheduler.schedule(cronExpress, task);
    log.debug("成功创建id为{}的{}定时任务", taskId, order);
    return taskId;
  }

  public String addTask(Task task) {
    String order = task.getOrderName();
    switch (order) {
      case Task.ORDER_ROLLCALL:
        if (!needMessage) {
          return "";
        }
        RollcallTask rollcallTask = new RollcallTask();
        rollcallTask.setNoticeProducer(noticeProducer);
        return this.addTask(task.getCronExpress(), rollcallTask, order);
      case Task.ORDER_SHIFT_START:
        if (!needMessage) {
          return "";
        }
        ShiftStartTask shiftStartTask = new ShiftStartTask();

        shiftStartTask.setTaskParam(task.getParam());
        shiftStartTask.setNoticeProducer(noticeProducer);
        return this.addTask(task.getCronExpress(), shiftStartTask, order);

      case Task.PRISONER_ACCOUNT_SYNCHRONIZE:
        PrisonerAccountSynchronizeTask prisonerAccountSynchronizeTask =
                new PrisonerAccountSynchronizeTask();
        prisonerAccountSynchronizeTask.setPrisonerAccountRepository(prisonerAccountRepository);
        prisonerAccountSynchronizeTask.setPrisonerRepository(prisonerRepository);
        prisonerAccountSynchronizeTask.setTaskRepository(taskRepository);
        return this.addTask(task.getCronExpress(), prisonerAccountSynchronizeTask, order);

      default:
        break;
    }
    return "";
  }

  public void delTask(String taskId) {
    log.debug("删除taskid={}的任务", taskId);
    scheduler.deschedule(taskId);
  }

  public List<String> getPrisonIdByParam(Map<String, Object> parm) {
    List<String> prisonIds = new ArrayList<>();
    if (parm == null) {
      return prisonIds;
    }
    String prisonIdStr = (String) parm.get("prisonIds");

    if (StringUtils.isNotBlank(prisonIdStr)) {
      for (String p : prisonIdStr.split(",")) {
        if (StringUtils.isNotBlank(p)) {
          prisonIds.add(p.trim());
        }
      }
    }
    return prisonIds;
  }

}

